package edu.scsu.csci.validator;

import java.lang.annotation.Annotation;

import edu.scsu.csci.IValidator;
import edu.scsu.csci.annotation.validator.ACharValidator;


/**
 * Validates the string object based on the criteria specified by <code>AStringValidator</code>.
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 */
final public class CharValidator implements IValidator<ACharValidator, Character>	{

	/**
	 * <p>Validates the passed String object based on the validator annotaion passesd.
	 * 
	 * @param annotation
	 * @param string
	 * @throws ValidationException 
	 */
	public void validate(ACharValidator annotation, Character value) throws ValidationException {
		if( isNull(annotation, value)){
			throw new ValidationException("field cannot be null");
		}
		if(isIllegalLength(annotation, value)){
			throw new ValidationException(annotation.errorMessage());
		}
	}
	
	/**
	 * Returns true if null is not an acceptable value and the string is null. 
	 * 
	 * @param annotation
	 * @param string
	 * @return
	 */
	private boolean isNull(ACharValidator annotation, Character value) {
		return annotation.isNullAllowed() ? false : value == null;
	}
	
	/**
	 * Returns true if the string of any other length than specified by the
	 * validator annotation.
	 * 
	 * @param annotation
	 * @param string
	 * @return
	 */
	private boolean isIllegalLength(ACharValidator annotation, Character value) {
		return value.equals(annotation.valid());
	}
	
	/**
	 * Retunrs true is can vlaidation can be performed for the passed object using the passed annotation.
	 * 
	 * @param type
	 * @param annotaionType
	 * @return
	 */
	public boolean canValidate(Class<?> type, Class<? extends Annotation> annotaionType) {
		if((type.getClass().equals(char.class) || type.getClass().equals(Character.class)) 
					&& ACharValidator.class.equals(annotaionType)){
			return true;
		}
		return false;
	}
	
}
